//
//  GSCommonLoginVerificationCodeButton.m
//  GSLoginDemo
//
//  Created by guoshikeji_a on 2018/7/30.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "GSCommonLoginVerificationCodeButton.h"

#import "GSCommonLoginPrefixProfile.h"

#import "GSCommonLoginVerificationCodeCountdownHandler.h"

#import "GSCommonLoginMMMaterialDesignSpinner.h"

@interface GSCommonLoginVerificationCodeButton () <GSCommonLoginVerificationCodeCountdownHandlerDelegate>  {
    NSTimeInterval _time;
}

@property (nonatomic, strong) GSCommonLoginVerificationCodeCountdownHandler *countdownHandler;


/**
 *GSCommonLoginMMMaterialDesignSpinner
 */
@property (nonatomic, strong) GSCommonLoginMMMaterialDesignSpinner *spinner;

@end

@implementation GSCommonLoginVerificationCodeButton

- (GSCommonLoginVerificationCodeCountdownHandler *)countdownHandler {
    if (!_countdownHandler) {
        _countdownHandler = [[GSCommonLoginVerificationCodeCountdownHandler alloc] initWithCountdownDelegate:self];
    }
    return _countdownHandler;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightRegular];
        _countdownTime = 60;
        _time = _countdownTime;
        //
        self.spinner = [GSCommonLoginMMMaterialDesignSpinner new];
        self.spinner.hidesWhenStopped = true;
        self.spinner.tintColor = GSLOGINRGBRGBHEX(0xD9D9D9);
        [self addSubview:self.spinner];
        [self.spinner mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_right).offset(5.0);
            make.centerY.mas_equalTo(self);
            make.width.height.mas_equalTo(self.titleLabel.font.lineHeight-4.0);
        }];
        
        
        //
        self.verifyCodeStatus = GSCommonLoginVerificationCodeStatusNormal;
    }
    return self;
}

- (void)setVerifyCodeStatus:(GSCommonLoginVerificationCodeStatus)verifyCodeStatus {
    _verifyCodeStatus = verifyCodeStatus;
    [self.countdownHandler endCountdownAction];
    [self.spinner stopAnimating];
    switch (verifyCodeStatus) {
        case GSCommonLoginVerificationCodeStatusNormal:
        {
            [self setTitleColor:GSLOGIN_THEME_COLOR forState:UIControlStateNormal];
            [self setTitle:GSLoginLocalized(@"Send Captcha") forState:UIControlStateNormal];
        }
            break;
        case GSCommonLoginVerificationCodeStatusWaiting:
        {
            [self setTitleColor:GSLOGINRGBRGBHEX(0xD9D9D9) forState:UIControlStateNormal];
            [self startCountdownAction];
        }
            break;
        case GSCommonLoginVerificationCodeStatusSending:
        {
            [self setTitleColor:GSLOGINRGBRGBHEX(0xD9D9D9) forState:UIControlStateNormal];
            [self.spinner startAnimating];
        }
            break;
        case GSCommonLoginVerificationCodeStatusReSend:
        {
            [self setTitleColor:GSLOGIN_THEME_COLOR forState:UIControlStateNormal];
            [self setTitle:GSLoginLocalized(@"Resend Captcha") forState:UIControlStateNormal];
        }
            break;
        default:
            break;
    }
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.verifyCodeStatus==GSCommonLoginVerificationCodeStatusReSend || self.verifyCodeStatus==GSCommonLoginVerificationCodeStatusNormal) {
        return [super pointInside:point withEvent:event];
    }
    return false;
}

- (void)startCountdownAction {
    _time = self.countdownTime;
    [self.countdownHandler startCountdownAction];
    [self loginVerificationCodeCountdownHandlerCountdownAction];
}

#pragma mark - GSCommonLoginVerificationCodeCountdownHandlerDelegate
- (void)loginVerificationCodeCountdownHandlerCountdownAction {
    if (_time<=0) {
        [_countdownHandler endCountdownAction];
        self.verifyCodeStatus = GSCommonLoginVerificationCodeStatusReSend;
        return;
    }
    NSString *string = [NSString stringWithFormat:@"%@（%@）",GSLoginLocalized(@"Resend Captcha"),@(_time)];
    [self setTitle:string forState:UIControlStateNormal];
    _time --;
}

- (void)dealloc {
    [_countdownHandler endCountdownAction];
    _countdownHandler = nil;
}

@end
